package com.cx.bank.view;

import com.cx.bank.factory.UserDaoFactory;
import com.cx.bank.manager.ManagerImpl;
import com.cx.bank.model.UserBean;
import com.cx.bank.util.InvalidDepositException;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

/**
 * @author jxau_scf
 * @date 2021/7/19 - 17:29
 */
public class IndexView extends JFrame {
    //查询余额,取 款,存 款,转账,退出系统
    private JButton inquiry, withdrawals, deposit, transfer, exitSystem;
    private JLabel bank;

    private static UserBean userBean = null;
//    UserDaoFactory userDaoFactory = null;
    private static ManagerImpl manager;

    public static ManagerImpl getManager() {
        return manager;
    }

    public static void setManager(ManagerImpl manager) {
        IndexView.manager = manager;
    }

    public IndexView(){
        building();
        addlisteners();
    }

    private void building() {
        setLayout(null);

        bank = new JLabel("bank");

        inquiry = new JButton("查询余额");
        withdrawals = new JButton("取 款");
        deposit = new JButton("存 款");
        transfer = new JButton("转 账");
        exitSystem = new JButton("退出系统");

        bank.setBounds(250,20,120,50);
        Font font = new Font("圆体", Font.BOLD, 50);
        Font font1 = new Font("圆体", Font.BOLD, 30);
        bank.setFont(font);

        inquiry.setBounds(250, 150, 120, 30);
        withdrawals.setBounds(250, 200, 120, 30);
        deposit.setBounds(250, 250, 120, 30);
        transfer.setBounds(250, 300, 120, 30);
        exitSystem.setBounds(250, 350, 120, 30);

        add(bank);
        add(inquiry);
        add(withdrawals);
        add(deposit);
        add(transfer);
        add(exitSystem);

        setBounds(600,600,600,600);
        setLocation((2000-getWidth())/2,(1000-getHeight())/2);
        setResizable(false);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setVisible(true);

        userBean = BankView.getUserBean();
        UserDaoFactory userDaoFactory = null;
        try {
            userDaoFactory = UserDaoFactory.getInstance();
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | IOException ex) {
            ex.printStackTrace();
        }
        manager = userDaoFactory.getManager();
        manager.setUserBean(userBean);
    }

    private void addlisteners() {
        //查询余额
        inquiry.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Double money = manager.inquiry();
                JOptionPane.showMessageDialog(inquiry, "剩余"+money+"元", "余额", JOptionPane.WARNING_MESSAGE);
            }
        });
        //取款
        withdrawals.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //打开输入金额页面
                new WithDrawalsMoneyView().setVisible(true);
//                Double money = WithDrawalsMoneyView.getMoney();
//                try {
////                    manager.withdrawals(money);
//                    manager.withdrawals(10.0);
//                } catch (InvalidDepositException ex) {
//                    ex.printStackTrace();
//                }
            }
        });
        //存款
        deposit.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                
            }
        });
    }


}
